
#include <QtGui/QTableWidgetItem>
#include <QtCore/QString>
#include <QtCore/Qt>

#include "newNetworkWizard.h"
#include "CNetworkManager.h"

/*----------------------------------------------------------------------------*/

NewNetworkWizard::NewNetworkWizard( CNetworkManager& _networkManager )
	:	m_ui( new Ui::NewNetworkWizardUi )
	,	m_networkManager( _networkManager )	
{
	m_ui->setupUi( this );
	
	/** Spin box: layers number */
	connect( m_ui->m_inputsNumSpinBox
		,	SIGNAL( valueChanged( const QString& ) )
		,	this
		,	SLOT( GetInputsNum( const QString& ) )
	);

	/** Spin box: layers number */
	connect( m_ui->m_layersNumSpinBox
		,	SIGNAL( valueChanged( int ) )
		,	this
		,	SLOT( GetLayersNum( int ) )
	);

	/** Spin box: outputs number */
	connect( m_ui->m_outputsNumSpinBox
		,	SIGNAL( valueChanged( const QString& ) )
		,	this
		,	SLOT( GetOutputsNum( const QString& ) )
	);

	/** Button: create */
	connect( m_ui->m_createNetButton
		,	SIGNAL( clicked() )
		,	this
		,	SLOT( NetworkCreate() )
	);
	
	/** other stuff */
}


/*----------------------------------------------------------------------------*/


NewNetworkWizard::~NewNetworkWizard()
{
	if ( m_ui )
		delete m_ui;
}


/*----------------------------------------------------------------------------*/


void NewNetworkWizard::ShowCentered( QWidget& _parent )
{
	/** move child window to the parent's center */
	this->move( _parent.frameGeometry().center() - this->rect().center() );
	this->show();
}


/*----------------------------------------------------------------------------*/


void NewNetworkWizard::NetworkCreate()
{
	IntV neuronsNum;
	for (int i = 0; i < m_ui->m_neuronsCountTable->rowCount(); i++ )
	{
		neuronsNum.push_back( m_ui->m_neuronsCountTable->item( i, 0 )->text().toInt() );
	}

	m_networkManager.CreateNetwork( m_ui->m_inputsNumSpinBox->value(), neuronsNum );

	emit NetworkAccessible( m_networkManager.NetworkExists() );

	this->hide();
}


/*----------------------------------------------------------------------------*/


void NewNetworkWizard::GetInputsNum( const QString& _inputs )
{	
	m_ui->m_neuronsCountTable->item( 0, 0 )->setText( _inputs );
}


/*----------------------------------------------------------------------------*/


void NewNetworkWizard::GetLayersNum( int _layers )
{	
	int rowCount = m_ui->m_neuronsCountTable->rowCount();	

	if( _layers >= rowCount )
	{
		m_ui->m_neuronsCountTable->setRowCount( _layers );
		for ( int i = rowCount; i < _layers; i++ )
		{
			m_ui->m_neuronsCountTable->setItem( i, 0, new QTableWidgetItem( m_ui->m_outputsNumSpinBox->text() ) );			
			EnableTableItem( i - 1 );
		}
		EnableTableItem( _layers - 1, false );
	}
	else
	{
		for ( int i = rowCount; i > _layers; i-- )
		{
			if ( m_ui->m_neuronsCountTable->item( i - 1, 0 ) )
				delete m_ui->m_neuronsCountTable->item( i - 1 , 0 );
		}
		m_ui->m_neuronsCountTable->setRowCount( _layers );
		/** after deleting rows - proper outputs number is set to last row */
		m_ui->m_neuronsCountTable->item( _layers - 1, 0 )->setText( m_ui->m_outputsNumSpinBox->text() );
		EnableTableItem( _layers - 1, false );
	}
}


/*----------------------------------------------------------------------------*/


void NewNetworkWizard::GetOutputsNum( const QString& _outputs )
{	
	int lastRow = m_ui->m_neuronsCountTable->rowCount() - 1;		
	m_ui->m_neuronsCountTable->item( lastRow, 0 )->setText( _outputs );	
}


/*----------------------------------------------------------------------------*/


void NewNetworkWizard::EnableTableItem( int _row, bool _state )
{
	Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable;
	m_ui->m_neuronsCountTable->item( _row, 0 )->setFlags( flags | ( _state ? Qt::ItemIsEnabled : Qt::NoItemFlags ) );	
}


/*----------------------------------------------------------------------------*/

